<?php

class Blog
{
	private $buffer;
	private $blogname;
	private $get = array();
	
	public function Blog($blogname)
	{
		$this->blogname = $blogname;
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, "www.gamer.ru/games/" . $this->blogname . "/posts");
		curl_setopt($curl, CURLOPT_HEADER, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		$this->buffer = curl_exec($curl);
		curl_close($curl);
		
		preg_match("#<h2 class=\"game_([0-9]+)_title main\">(.+)</h2>#i", $this->buffer, $matches);	
		$this->blogname = $matches[2];
		
		preg_match("#За блогом (следит|следят) <strong>([0-9]+)</strong>#i", $this->buffer, $matches);
		$this->get['follow_text'] = $matches[1];
		$this->get['follow'] = $matches[2];
		
		preg_match("#<strong>([0-9]+)</strong> <a href=\"http://www.gamer.ru/games/(.+)/posts/silver#i", $this->buffer, $matches);
		$this->get['silver'] = $matches[1];			
		preg_match("#<strong>([0-9]+)</strong> <a href=\"http://www.gamer.ru/games/(.+)/posts/golden#i", $this->buffer, $matches);
		$this->get['golden'] = $matches[1];		
		preg_match("#<strong>([0-9]+)</strong> <a href=\"http://www.gamer.ru/games/(.+)/posts/brilliant#i", $this->buffer, $matches);
		$this->get['diamond'] = $matches[1];
		preg_match("#<strong>([0-9]+)</strong> <a href=\"http://www.gamer.ru/games/(.+)/posts/recommended#i", $this->buffer, $matches_rec);
		if(count($matches_rec)) $this->get['recommend'] = $matches_rec[1];					
		else $this->get['recommend'] = 0;
		
		preg_match("#<span class=\"(not_top10|first|second|third|top10)\">#i", $this->buffer, $matches);
		if(!count($matches)) 
		{ 
			$this->get['place'] = -1; 
		}
		else if($matches[1] == 'not_top10' || $matches[1] == 'top10')
		{
			preg_match("#<span class=\"(not_top10|first|second|third|top10)\">([0-9]+)-е место#i", $this->buffer, $matches);
			$this->get['place'] = $matches[2];
		}
		else if($matches[1] == 'first') { $this->get['place'] = 1; }
		else if($matches[1] == 'second') { $this->get['place'] = 2; }
		else if($matches[1] == 'third') { $this->get['place'] = 3; }

	}

	public function GetName() 
	{ 
		return $this->blogname; 
	}
	
	public function GetFollow() 
	{ 
		return $this->get['follow_text'] . " " . $this->get['follow'] . " чел."; 
	}
		
	public function GetPlace() 
	{ 
		return $this->get['place'];
	}
	
	public function GetPosts($type) 
	{ 
		switch($type)
		{
			case 'diamond':
			{
				return $this->get['diamond']; 
			}
			break;
			case 'golden':
			{
				return $this->get['golden']; 
			}
			break;		
			case 'silver':
			{
				return $this->get['silver']; 
			}
			break;
			case 'recommend':
			{
				return $this->get['recommend']; 
			}
			break;			
			default:
			{
				return $this->get['posts']; 
			}
			break;
		}
	}
}

?>